package com.creditease.rss.ui;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.creditease.rss.RssConst;

public class SettingActivity extends Activity {

	private final static String TAG = SettingActivity.class.getName();
	private ListView listView;

	private static final int MSG_SUCCESS = 0;//
	private static final int MSG_FAILURE = 1;//
	private static final int MSG_LOAD = 2;//
	private static final int DATA_MEPTY = 3;//

	private Handler handler;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		this.setTitle(R.string.menu_setting);
		listView = (ListView) findViewById(R.id.setting_listView);
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		Map<String, String> map = new HashMap<String, String>();

		map.put("setting_title", "清除缓存");
		//map.put("setting_desc", "缓存大小：" + getFileLength());
		list.add(map);

		SimpleAdapter adapter = new SimpleAdapter(	getApplicationContext(),
													list,
													R.layout.setting_item,
													new String[]{"setting_title", "setting_desc"},
													new int[]{R.id.setting_title, R.id.setting_desc});
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
				HashMap item = (HashMap) parent.getItemAtPosition(position);
				Log.d(TAG, "position="+position);
				if (position == 0) {
					File file = new File(RssConst.PIC_TMP_DIR);

					File[] tmpFiles = file.listFiles();
					for (File f : tmpFiles) {
						f.delete();
					}
					Toast.makeText(getApplicationContext(), "清除缓存成功", Toast.LENGTH_SHORT).show();
				}
			}
		});

	}

	private String getFileLength() {

		File file = new File(RssConst.PIC_TMP_DIR);
		File[] tmpFiles = file.listFiles();
		
		long size = 0;
		for (File f : tmpFiles) {
			Log.d(TAG, "==============="+f.getPath()+",size="+f.length());
			size += f.length();
		}
		return size / (1024 * 1024) + "M";
	}
}